Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next

Allocating Memory at Startup Time

If you are implementing a system extension, you might need to allocate memory at startup time. As explained in the previous section, an ideal place to allocate such memory is in the system heap. To allocate memory in the system heap under system software version 7.0 and later, you merely need to call the appropriate Memory Manager routines, and the system heap expands dynamically to meet your request. In earlier versions of system software, you must use a 'sysz' resource to indicate how much the Operating System should increase the size of the system zone.

Alternatively, however, you can allocate blocks in high memory. The global variable BufPtr always references the highest byte in memory that might become part of an application partition. You can lower the value of BufPtr and then use the memory between the old and new values of BufPtr .

In general, if you are implementing a system extension, you should allocate memory in the system heap instead of high memory. In this way, you avoid the problems associated with lowering the value of BufPtr too far (described in the following paragraphs) and ensure that the extension is not paged out if virtual memory is operating.

Lowering the value of BufPtr too far can be dangerous for several reasons. In 128K ROM Macintosh computers running system software version 4.1, you must avoid lowering the value of BufPtr so that it points in the system startup blocks. The highest byte of these blocks can always be found relative to the global variable MemTop , at MemTop DIV 2 + 1024 .

In later versions of the Macintosh system software, the system startup blocks were no longer barriers to BufPtr , but new barriers arose, including Macintosh IIci video storage, for example. To maintain compatibility with extensions that rely on the ability to lower BufPtr relative to MemTop , the system software simply adjusts MemTop so that the formula still holds. Thus, at startup, the MemTop global variable currently does not reference any memory location in particular. Instead, it holds a value that guarantees that the formula allowing you to lower BufPtr as low as MemTop DIV 2 + 1024 but no further still holds.

Beginning in system software version 7.0, the Operating System can detect excessive lowering of BufPtr , but only after the fact. When the Operating System does detect that the value of BufPtr has fallen too low, it generates an out-of-memory system error.

Although the above formula has been true since system software version 4.1, a bug in the Macintosh IIci and later ROMs made it invalid in certain versions of system software 6.x.

Because there is no calling interface for lowering BufPtr , you must do it manually, by changing the value of the system variable, as explained in "Reading and Writing System Global Variables" . To obtain the value of the MemTop global variable, you can use the TopMem function.


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next